package cn.lx.auth.secutity.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.util.NumberUtils;
import org.springframework.util.StringUtils;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @ClassName SessionConfig
 * @Description: //todo
 * @Author: 落雪
 * @CreateDate: 2025/7/14 23:19
 * @UpdateUser: 落雪
 * @UpdateDate: 2025/7/14 23:19
 * @UpdateRemark:
 * @Version: 1.0
 */
@Configuration
//@EnableRedisHttpSession
public class SessionConfig {
    @Value("${security.type:}")
    private String sessionType;
    static final String SESSION_TYPE_SSO = "hsa-sso";
    @Value("${session.redis.host:}")
    private String host;
    @Value("${session.redis.port:0}")
    private int port;
    @Value("${session.redis.database:0}")
    private int database;
    @Value("${session.redis.password:}")
    private String password;
    @Value("${session.redis.timeout:}")
    private Integer timeout;
    @Value("${session.redis.cluster.nodes:}")
    private String nodes;
    @Value("${session.redis.cluster.max-redirects:}")
    private String maxRedirects;
    @Value("${session.redis.pool.maxIdle:}")
    private Integer maxIdle;
    @Value("${session.redis.pool.maxTotal:}")
    private Integer maxTotal;
    @Value("${session.redis.pool.maxWaitMillis:}")
    private Long maxWaitMillis;
    @Value("${session.redis.pool.testOnBorrow:true}")
    private boolean testOnBorrow;
    @Value("${session.redis.pool.testOnReturn:true}")
    private boolean testOnReturn;

    public SessionConfig() {
    }

    public JedisConnectionFactory getRedisConnectionFactory() {
        JedisConnectionFactory jedisConnectionFactory = null;
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        if (this.maxIdle != null) {
            jedisPoolConfig.setMaxIdle(this.maxIdle);
        }

        if (this.maxTotal != null) {
            jedisPoolConfig.setMaxTotal(this.maxTotal);
        }

        if (this.maxWaitMillis != null) {
            jedisPoolConfig.setMaxWaitMillis(this.maxWaitMillis);
        }

        jedisPoolConfig.setTestOnBorrow(this.testOnBorrow);
        jedisPoolConfig.setTestOnReturn(this.testOnReturn);
        if (StringUtils.isEmpty(this.nodes)) {
            if (!"hsa-sso".equals(this.sessionType) && (this.host == null || "".equals(this.host))) {
                this.host = "localhost";
            }

            RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(this.host, this.port);
            redisStandaloneConfiguration.setDatabase(this.database);
            redisStandaloneConfiguration.setPassword(RedisPassword.of(this.password));
            jedisConnectionFactory = new JedisConnectionFactory(redisStandaloneConfiguration);
            jedisConnectionFactory.setUsePool(true);
            jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
        } else {
            if (!"hsa-sso".equals(this.sessionType) && (this.nodes == null || "".equals(this.nodes))) {
                this.nodes = "localhost";
            }

            RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(StringUtils.commaDelimitedListToSet(this.nodes));
            if (!StringUtils.isEmpty(this.maxRedirects)) {
                redisClusterConfiguration.setMaxRedirects((Integer) NumberUtils.parseNumber(this.maxRedirects, Integer.class));
            }

            redisClusterConfiguration.setPassword(RedisPassword.of(this.password));
            jedisConnectionFactory = new JedisConnectionFactory(redisClusterConfiguration, jedisPoolConfig);
        }

        jedisConnectionFactory.afterPropertiesSet();
        if (jedisConnectionFactory != null && this.timeout != null) {
            jedisConnectionFactory.setTimeout(this.timeout);
        }

        return jedisConnectionFactory;
    }
}

